<?php

	class EventUserDataAccessMySQL implements iEventUserDataAccess{
		
		public function buildObject($result) {
			
			$eventUser = new EventUser();
			
			$user = new User();
			$user->setUserId($result->userId);
			$user->setEmail($result->email);
			$user->setName($result->userName);
			
			$invitationUser = new User();
			$invitationUser->setUserId($result->invitationUserId);
			$invitationUser->setName($result->invitationUserName);
			
			$eventUser->setInvitationUser($invitationUser);
			$eventUser->setUser($user);
			$eventUser->setIsUserManager($result->isManager);
			$eventUser->setIsUserConfirmed($result->isConfirmed);
			$eventUser->setUserRegistrationCode($result->registrationCode);
						
			
			
			return $eventUser;
		}
		
		public function selectEventUserByEventId($eventId, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["EVENTID"]    = $eventId;
			
			$query = buildQuery("querySelectEventUserByEventId", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function selectNotInvitedUserByEventId($eventId, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["EVENTID"]    = $eventId;
			
			$query = buildQuery("querySelectNotInvitedUserByEventId", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		
		public function saveEventAttendant($event, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["EVENTID"]    = $event->getEventId();
			$parameters["USERID"]    = $event->getEventUser()->getUser()->getUserId();
			
			if ($event->getEventUser()->getInvitationUser()->getUserId() != null){
				$parameters["INVITATIONUSERID"] = $event->getEventUser()->getInvitationUser()->getUserId();
			}else {
				$parameters["INVITATIONUSERID"] = "NULL";
			}
			
			if ($event->getEventUser()->getIsUserConfirmed()){
				$parameters["ISCONFIRMED"] = "true";
			}else {
				$parameters["ISCONFIRMED"] = "false";
			}
			
			if ($event->getEventUser()->getIsUserManager()){
				$parameters["ISMANAGER"] = "true";
			}else {
				$parameters["ISMANAGER"] = "false";
			}
			$query = buildQuery("queryInsertEventAttendant", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
		}
		
		public function updateEventAttendant($event, $dataBaseConnection) {
									
			$parameters["EVENTID"]    = $event->getEventId();
			$parameters["USERID"]    = $event->getEventUser()->getUser()->getUserId();
						
			if ($event->getEventUser()->getIsUserConfirmed()){
				$parameters["ISCONFIRMED"] = "true";
			}else {
				$parameters["ISCONFIRMED"] = "false";
			}
			$query = buildQuery("queryUpdateEventAttendant", $parameters);
			
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
		}
		
		public function deleteEventAttendant($event, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["REGISTRATIONCODE"]    = $event->getEventUser()->getUserRegistrationCode();
					
			$query = buildQuery("queryDeleteEventAttendant", $parameters);
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
		}
	}

?>